﻿using System;
using System.Collections.Generic;
using System.Text;

namespace SharpSoft.Data.DAOS
{
    public class DistrictAreaDAO : DAO
    {
        protected override string InitScriptFileName => "districtArea.txt";

        public DistrictAreaDAO(Database p_db, int p_userid) : base(p_db, p_userid)
        {
        }
        /// <summary>
        /// 获取指定级别的区域列表
        /// </summary>
        /// <param name="level"></param>
        /// <returns></returns>
        public DistrictAreaInfo[] GetLevel(DistrictAreaLevel level)
        {
            string sql = $"SELECT * FROM `Data_districtarea` WHERE `level`={(int)level};";
            return db.ExecuteArray<DistrictAreaInfo>(sql);
        }
        /// <summary>
        /// 获取区域的完整名称
        /// </summary>
        /// <param name="areaId"></param>
        /// <returns></returns>
        public string[] GetFullName(string areaCode)
        {
            List<string> list = new List<string>();
            while (true)
            {
                var area = GetArea(areaCode);
                if (area == null)
                {
                    break;
                }
                else
                {
                    list.Add(area.areaName);
                    areaCode = area.parentCode;
                    if (string.IsNullOrEmpty(areaCode))
                    {
                        break;
                    }
                }
            }
            list.Reverse();
            return list.ToArray();
        }
        /// <summary>
        /// 获取区域信息
        /// </summary>
        /// <param name="areaId"></param>
        /// <returns></returns>
        public DistrictAreaInfo GetArea(string areaCode)
        {
            string sql = $"SELECT * FROM `Data_districtarea` WHERE `areaCode`=@areaCode;";
            return db.ExecuteObject<DistrictAreaInfo>(sql, new { areaCode });
        }

        /// <summary>
        /// 获取子区域
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        public DistrictAreaInfo[] GetChildren(string parentCode)
        {
            string sql = $"SELECT * FROM `Data_districtarea` WHERE `parentCode`=@parentCode;";
            return db.ExecuteArray<DistrictAreaInfo>(sql, new { parentCode = parentCode });
        }
        /// <summary>
        /// 更新行政区域数据
        /// </summary>
        /// <param name="area"></param>
        /// <param name="children"></param>
        public void UpdateDistrictArea(DistrictAreaInfo area, IEnumerable<DistrictAreaInfo> children)
        {
            string sql = @"INSERT INTO `Data_districtarea`(`areaCode`,`areaName`,`level`,`cityCode`,`center`,`parentCode`,`updateTime`)VALUES(@areaCode,@areaName,@level,@cityCode,@center,@parentCode,NOW())
ON DUPLICATE KEY UPDATE `areaCode`= VALUES(`areaCode`),`areaName`= VALUES(`areaName`),`level`= VALUES(`level`),`cityCode`= VALUES(`cityCode`),`center`= VALUES(`center`),`updateTime`=VALUES(`updateTime`); ";
            db.ExecuteNonQuery(sql, area);//更新本节点
            if (children != null)
            {
                foreach (var item in children)
                {
                    item.parentCode = area.areaCode;
                    string sqlc = @"INSERT INTO `Data_districtarea`(`areaCode`,`areaName`,`level`,`cityCode`,`center`,`parentCode`,`updateTime`)VALUES(@areaCode,@areaName,@level,@cityCode,@center,@parentCode,'1990-01-01')
ON DUPLICATE KEY UPDATE `areaCode`=VALUES(`areaCode`),`areaName`=VALUES(`areaName`),`level`=VALUES(`level`),`cityCode`=VALUES(`cityCode`),`center`=VALUES(`center`),`parentCode`=VALUES(`parentCode`);";
                    db.ExecuteNonQuery(sqlc, item);//更新子节点， 新插入子节点的更新时间设为很久以前以便于下次访问子节点时更新节点信息
                }

            }


        }
        /// <summary>
        /// 获取父区域码
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public string GetParentCode(string code)
        {
            var sql = "SELECT `parentCode` FROM `Data_districtarea` WHERE `areaCode`=@code";
            return db.ExecuteString(sql, new { code });
        }
    }
    public enum DistrictAreaLevel
    {
        /// <summary>
        /// 未指定
        /// </summary>
        None = -99,
        /// <summary>
        /// 国家
        /// </summary>
        Country = 0,
        /// <summary>
        /// 省份
        /// </summary>
        Province = 1,
        /// <summary>
        /// 城市
        /// </summary>
        City = 2,
        /// <summary>
        /// 区县
        /// </summary>
        District = 3,
        /// <summary>
        /// 街道
        /// </summary>
        Street = 4,
    }

    public class DistrictAreaInfo
    {
        public string areaCode { get; set; }
        public string parentCode { get; set; }
        public string areaName { get; set; }
        public DistrictAreaLevel level { get; set; } = DistrictAreaLevel.None;
        public string cityCode { get; set; }
        public string center { get; set; }
        public DateTime updateTime { get; set; }
    }
}
